﻿using Coldairarrow.Business.Base_Manage;
using Coldairarrow.Business.Base_leasesystem_servicebank;
using Coldairarrow.IBusiness;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using NSwag.Annotations;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Coldairarrow.Util;
using Coldairarrow.Business.Base_leasesystem_serviceaccount;
using Coldairarrow.Business.Base_leasesystem_service;
using Coldairarrow.Business.Base_leasesystem_agent;
using Coldairarrow.Business.Base_leasesystem_merchant;

namespace Coldairarrow.Api.Controllers.Base_Manage
{
    /// <summary>
    /// 首页控制器
    /// </summary>
    [Route("/Base_Manage/[controller]/[action]")]
    [OpenApiTag("主页")]
    public class HomeController : BaseApiController
    {
        readonly Ileasesystem_serviceaccountBusiness _serviceaccount;
        readonly Ileasesystem_merchantBusiness _MerchantBusiness;
        readonly Ileasesystem_agentBusiness _agent;
        readonly Ileasesystem_serviceBusiness _service;
        readonly IHomeBusiness _homeBus;
        readonly IPermissionBusiness _permissionBus;
        readonly IBase_UserBusiness _userBus;
        readonly IOperator _operator;
        private readonly JwtOptions _jwtOptions;
        public HomeController(
            Ileasesystem_merchantBusiness merchantBusiness,//客户业务类
            Ileasesystem_serviceaccountBusiness serviceaccount,//服务商银行账号
            Ileasesystem_agentBusiness agent,//渠道商
            Ileasesystem_serviceBusiness service,//服务商
            IHomeBusiness homeBus,
            IPermissionBusiness permissionBus,
            IBase_UserBusiness userBus,
            IOperator @operator,
            IOptions<JwtOptions> jwtOptions
            )
        {
            _MerchantBusiness = merchantBusiness;
            _agent = agent;
            _service = service;
            _serviceaccount = serviceaccount;
            _homeBus = homeBus;
            _permissionBus = permissionBus;
            _userBus = userBus;
            _operator = @operator;
            _jwtOptions = jwtOptions.Value;
        }

        /// <summary>
        /// 用户登录(获取token)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<string> SubmitLogin(LoginInputDTO input)
        {
            var userId = await _homeBus.SubmitLoginAsync(input);

            var claims = new[]
            {
                new Claim("userId",userId)
            };
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtOptions.Secret));
            var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var jwtToken = new JwtSecurityToken(
                string.Empty,
                string.Empty,
                claims,
                expires: DateTime.Now.AddHours(_jwtOptions.AccessExpireHours),
                signingCredentials: credentials);

            return new JwtSecurityTokenHandler().WriteToken(jwtToken);
        }
        [HttpPost]
        public async Task ChangePwd(ChangePwdInputDTO input)
        {
            await _homeBus.ChangePwdAsync(input);
        }

        [HttpPost]
        public async Task<object> GetOperatorInfo()
        {
            var theInfo = await _userBus.GetTheDataAsync(_operator.UserId);
            var permissions = await _permissionBus.GetUserPermissionValuesAsync(_operator.UserId);
            var resObj = new
            {
                UserInfo = theInfo,
                Permissions = permissions
            };

            return resObj;
        }

        [HttpPost]
        public async Task<List<Base_ActionDTO>> GetOperatorMenuList()
        {
            return await _permissionBus.GetUserMenuListAsync(_operator.UserId);
        }
        /// <summary>
        /// 根据服务商ID查询银行账户
        /// </summary>
        /// <param name="input">服务商ID</param>
        /// <returns></returns>
        [HttpPost]
        public object GetBankBySID(IdInputDTO input)
        {
            var obj = _serviceaccount.GetBankBySID(input.id);
            return obj;
        }

        /// <summary>
        /// 获取服务商名称及id   填充选择框
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object GetService_Select() 
        {
            return _service.GetService_Select();
        }

        /// <summary>
        /// 获取渠道/代理商的名称及ID   选择框
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object GetAgent_Select()
        {
            return _agent.GetAgent_Select();
        }

        /// <summary>
        /// 获取用户表/客户经理的名称及ID   0 全部数据  1 去除当前获取者数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object GetUser_Select(string IsAll)
        {
            return _userBus.GetUser_Select(IsAll);
        }

        /// <summary>
        /// 获取所有出租人    用于选择框
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object GetEmployess() 
        {
            return _userBus.GetEmployess();
        }

        /// <summary>
        /// 根据客户ID/企业ID 获取客户余额  总充值金额  客户总数
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object GetMerchantInfo( PageInput<ConditionDTO> input)
        {
            return _homeBus.GetMerchantInfo(input);
        }

        /// <summary>
        /// 获取所有客户    下拉框绑定
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object GetMerchant_Select()
        {
            return _MerchantBusiness.FullSelect();
        }

        /// <summary>
        /// 获取交易统计数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object GetTran_statiscs(PageInput<ConditionDTO> input) 
        {
           return _homeBus.GetTran_statiscs(input);
        }

        /// <summary>
        /// 获取我的账户中客户信息页面数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<object> GetAccount_Merchant() 
        {
            return await _homeBus.GetAccount_Merchant(_operator.UserId);
        }

        /// <summary>
        /// 获取字典类型   下拉框
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<object> GetDic_type() 
        {
            return await _homeBus.GetDic_type();
        }

        /// <summary>
        /// 获取字典值   下拉框
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<object> GetDic_value()
        {
            return await _homeBus.GetDic_value();
        }

        /// <summary>
        /// 代付通道获取    下拉框
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<object> GetBank_Code() 
        {
            return await _homeBus.GetBank_Code();
        }

        //[HttpPost]
        //public async Task<object> GetAlipaySign()
        //{
        //    return await GetAlipaySign();
        //}

        /// <summary>
        /// 根据服务商ID 获取开户行
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<object> GetServiceBank(PageInput<ConditionDTO> input)
        {
            return await _homeBus.GetServiceBank(input);
        }

        /// <summary>
        /// 获取主页数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<object> GetHomeData(PageInput<ConditionDTO> input)
        {
           return await  _homeBus.GetHomeData(input,_operator.UserId);
        }

        [HttpPost]
        public async Task SendMessage()
        {
            _homeBus.SendMessage();
        }

    }
}